package office.service.business.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import office.action.business.form.ContractConditionForm;
import office.action.business.form.ContractForm;
import office.action.business.wrapper.ContractWrapper;
import office.bo.business.impl.Contract;
import office.domainset.BizDomainProviderSetImpl;
import office.service.business.IContractService;
import platform.common.bo.Page;
import platform.common.dao.IDomainProvider;
import platform.common.exception.BusinessException;
import platform.common.service.impl.BaseBizServiceImpl;
import platform.util.DateUtil;
import platform.util.Util;
import current.security.user.domain.User;

public class ContractServiceImpl extends BaseBizServiceImpl implements IContractService{
	
	public void editContract(ContractForm form)throws BusinessException{
		try {
			BizDomainProviderSetImpl domainProviderSetImpl = (BizDomainProviderSetImpl) domainProviderSet;
			Contract bo = (Contract)domainProviderSetImpl.getContractDP().newEmptyObj();
			bo =(Contract) form.copyPropertiesToBO(bo);
			if(null==bo.getId() || ("").equals(bo.getId())){
				bo.generatorUUID();
				bo.setIsSign("0");
				bo.setIsStockUp("0");
				bo.setIsDelivery("0");
				bo.setIsInvoice("0");
			}
			//验证是否存在重复记录
			bo.store(domainProviderSet);
		} catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("编辑合同失败!", e);
		}
	}
	
	public Page getPageInfo(ContractConditionForm condition)throws BusinessException{
		Page page = new Page();
		try{
			BizDomainProviderSetImpl domainProviderSetImpl=(BizDomainProviderSetImpl)domainProviderSet;
			IDomainProvider provider = domainProviderSetImpl.getContractDP();
			int pageSize = Integer.parseInt(Util.parsePropertiesReturnStr("project", "Contract.pageSize"));
			page.setPageSize(pageSize);
			Map condMap= new HashMap();
			if(null!=condition&&!Util.isNull(condition.getContractName())){
				condMap.put("contractName", condition.getContractName());
			}
			if(null!=condition&&!Util.isNull(condition.getContractCode())){
				condMap.put("contractCode", condition.getContractCode());
			}
			if(null!=condition&&!Util.isNull(condition.getContractType())){
				condMap.put("contractType", condition.getContractType());
			}
			if(null!=condition&&!Util.isNull(condition.getCreateMan())){
				condMap.put("createMan", condition.getCreateMan());
			}
			if(null!=condition&&!Util.isNull(condition.getProjectUnit())){
				condMap.put("projectUnit", condition.getProjectUnit());
			}
			if(null!=condition&&!Util.isNull(condition.getSalesMan())){
				condMap.put("salesMan", condition.getSalesMan());
			}
			if(null!=condition&&!Util.isNull(condition.getSignDate())){
				condMap.put("signDate", DateUtil.dateToString(condition.getSignDate()));
			}
			//结束时间
			if(null!=condition&&!Util.isNull(condition.getEndSignDate())){
				condMap.put("endSignDate", DateUtil.dateToString(condition.getEndSignDate()));
			}
			
			List list = domainProviderSetImpl.getDao().findByDynamicHql(provider, "findContractByParam", condMap,new HashMap());
			if(list.size()%pageSize == 0){
				page.setTotalPage(list.size()/pageSize);
			}else{
				page.setTotalPage(list.size()/pageSize+1);
			}
			
		}catch(Exception e){
			log.error("", e);
			this.handleBusinessServiceException("获得分页信息失败", e);
		}	
		return page;
	}
	
	public List<ContractWrapper> findAllContract(ContractConditionForm condition,Page page)throws BusinessException{
		List<ContractWrapper> wrapperList = new ArrayList();
		try{
			BizDomainProviderSetImpl domainProviderSetImpl=(BizDomainProviderSetImpl)domainProviderSet;
			IDomainProvider provider = domainProviderSetImpl.getContractDP();
			Map condMap= new HashMap();
			if(null!=condition&&!Util.isNull(condition.getContractName())){
				condMap.put("contractName", condition.getContractName());
			}
			if(null!=condition&&!Util.isNull(condition.getContractCode())){
				condMap.put("contractCode", condition.getContractCode());
			}
			if(null!=condition&&!Util.isNull(condition.getContractType())){
				condMap.put("contractType", condition.getContractType());
			}
			if(null!=condition&&!Util.isNull(condition.getCreateMan())){
				condMap.put("createMan", condition.getCreateMan());
			}
			if(null!=condition&&!Util.isNull(condition.getProjectUnit())){
				condMap.put("projectUnit", condition.getProjectUnit());
			}
			if(null!=condition&&!Util.isNull(condition.getSalesMan())){
				condMap.put("salesMan", condition.getSalesMan());
			}
			if(null!=condition&&!Util.isNull(condition.getSignDate())){
				condMap.put("signDate", DateUtil.dateToString(condition.getSignDate()));
			}
			if(null!=condition&&!Util.isNull(condition.getEndSignDate())){
				condMap.put("endSignDate", DateUtil.dateToString(condition.getEndSignDate()));
			}
			List list = domainProviderSetImpl.getDao().findByDynamicHql(provider,"findContractByParam",condMap,new HashMap(),page);
			ContractWrapper wrapper;
			for(int i=0;i<list.size();i++){
				Contract bo = (Contract)list.get(i);
				User user=bo.getUser(domainProviderSetImpl);
				if(null!=user){
					bo.setSalesManName(user.getCname());
				}
				wrapper = new ContractWrapper();
				wrapper.copyToWrapper(bo);
				wrapperList.add(wrapper);
			}
		}catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("获取合同列表失败！", e);
		}
		return wrapperList;
	}

	public void delContractById(String id)throws BusinessException{
		try{
			BizDomainProviderSetImpl domainProviderSetImpl=(BizDomainProviderSetImpl)domainProviderSet;
			IDomainProvider provider = domainProviderSetImpl.getContractDP();
			domainProviderSetImpl.getDao().deleteById(provider, id);
		}catch(Exception e){
			log.error("", e);
			throw this.handleBusinessServiceException("删除合同失败！", e);
		}
	}
	
	public ContractForm getById(String id)throws BusinessException{
		ContractForm form = new ContractForm();
		try {
			BizDomainProviderSetImpl domainProviderSetImpl = (BizDomainProviderSetImpl) domainProviderSet;
			Contract bo = (Contract)domainProviderSetImpl.getDao().findByIdAndDomain(domainProviderSetImpl.getContractDP(), id);
			form.copyPropertiesFromBO(bo);
		} catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("根据id获取合同失败!", e);
		}
		return form;
	}
	public List<ContractWrapper> findAllContractByStatus(ContractConditionForm condition)throws BusinessException{
		List<ContractWrapper> wrapperList = new ArrayList();
		try{
			BizDomainProviderSetImpl domainProviderSetImpl=(BizDomainProviderSetImpl)domainProviderSet;
			IDomainProvider provider = domainProviderSetImpl.getContractDP();
			Map condMap= new HashMap();
			/*if(null!=condition&&!Util.isNull(condition.getStatus())){
				condMap.put("isSign", condition.getStatus());
			}*/
			List list = domainProviderSetImpl.getDao().findByDynamicHql(provider,"findContractByStatus",condMap,new HashMap());
			ContractWrapper wrapper;
			for(int i=0;i<list.size();i++){
				Contract bo = (Contract)list.get(i);
				User user=bo.getUser(domainProviderSetImpl);
				if(null!=user){
					bo.setSalesManName(user.getCname());
				}
				wrapper = new ContractWrapper();
				wrapper.copyToWrapper(bo);
				wrapperList.add(wrapper);
			}
		}catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("获取合同列表失败！", e);
		}
		return wrapperList;
	}
	
	public void invoiceGoodsOrder(String id) throws BusinessException{
		try {
			BizDomainProviderSetImpl domainProviderSetImpl = (BizDomainProviderSetImpl) domainProviderSet;
			Contract bo = (Contract)domainProviderSetImpl.getDao().findByIdAndDomain(domainProviderSetImpl.getContractDP(), id);
			bo.setIsInvoice("1");
			bo.store(domainProviderSetImpl);
			
		} catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("对合同进行开发票失败!", e);
		}
	}
	public void signContract(String id) throws BusinessException{
		try {
			BizDomainProviderSetImpl domainProviderSetImpl = (BizDomainProviderSetImpl) domainProviderSet;
			Contract bo = (Contract)domainProviderSetImpl.getDao().findByIdAndDomain(domainProviderSetImpl.getContractDP(), id);
			bo.setIsSign("1");
			bo.store(domainProviderSetImpl);
			
		} catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("对合同进行签署失败!", e);
		}
	}
	
	public void backMoneyContract(String id) throws BusinessException{
		try {
			BizDomainProviderSetImpl domainProviderSetImpl = (BizDomainProviderSetImpl) domainProviderSet;
			Contract bo = (Contract)domainProviderSetImpl.getDao().findByIdAndDomain(domainProviderSetImpl.getContractDP(), id);
			bo.setRemitMoney(bo.getContractMoney());
			bo.store(domainProviderSetImpl);
			
		} catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("对合同进行回款失败!", e);
		}
	}
	
}
